package com.ys.controller.api.business;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.entity.business.TpDeptUserPoint;
import com.ys.service.business.TpDeptUserPointService;
import com.ys.service.flow.YspTaskService;
import com.ys.util.shiro.ShiroUtils;
import com.ys.vo.Flow;
import com.ys.vo.R;

@Controller
@RequestMapping("/api/inter/tpDeptUserPoint")
public class ApiInterTpDeptUserPointController {
    @Autowired
    private TpDeptUserPointService tpDeptUserPointService;
    @Autowired
    private YspTaskService yspTaskService;

    /**
     * 删除
     */
    @PostMapping("/deleteDeptUserPoint")
    @ResponseBody
    public R deleteDeptUserPoint(HttpServletRequest request, HttpServletResponse response,
                                 Long id) throws Exception {
        return tpDeptUserPointService.deleteDeptUserPoint(id);
    }



    /**
     * 流转通过
     */
    @RequiresPermissions("tpDeptUserPoint:edit")
    @PostMapping("/flowPassDeptUserPoint")
    @ResponseBody
    public R flowPassDeptUserPoint(HttpServletRequest request, Long id){

        TpDeptUserPoint entity = new TpDeptUserPoint().setId(id).queryFirst();
        String bid = entity.getBid();
        try {
            Flow f = yspTaskService.flowPass(bid, ShiroUtils.getUserId());
            if (f.getCode() == Flow.Code.FAILED.value) {
                return R.error(f.getMsg());
            } else {
                tpDeptUserPointService.flowFinish(id);
                return R.ok();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("服务器正在开小差，请联系客服");
        }
    }


    /**
     * 流转驳回
     */
    @RequiresPermissions("tpDeptUserPoint:edit")
    @PostMapping("/flowRefuse")
    @ResponseBody
    public R flowRefuse(HttpServletRequest request, Long id, String reason){

        TpDeptUserPoint entity = new TpDeptUserPoint().setId(id).queryFirst();
        String bid = entity.getBid();
        try {
            return yspTaskService.flowRefuse(bid, reason, ShiroUtils.getUserId());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("服务器正在开小差，请联系客服");
        }
    }

}